<template>
<div>
    <el-dialog 
        title="文件分享" 
        :visible.sync="$store.state.share_dialog" 
        append-to-body
        width="500px">
        <div class="container">
            <div class="left">
                <div class="top">
                    <div class="left">
                        <List_item_cp
                            v-for="(item,index) in $store.state.selection_files"
                            :key="index"
                            :data_obj="item">
                        </List_item_cp>
                    </div>
                </div>
                <el-form ref="share_form" 
                    :model="share_form" 
                    label-position="left"
                    label-width="110px">
                    <div class="centre">
                        <el-form-item 
                            label="分享的用户"
                            prop="beUser"
                            :rules="[
                                { required: true, message: '不能为空'}, 
                            ]">
                            <el-input v-model="share_form.beUser" placeholder="多用户用,号分割"></el-input>
                        </el-form-item>
                    </div>
                    <el-checkbox v-model="checked_data">保留文件原有关键字</el-checkbox>
                    <div class="bottom">
                        <el-form-item 
                            label="请输入关键字"
                            prop="keyWord"
                            :rules="[
                                { validator: test_file_keyword, trigger: 'blur'}, 
                            ]">
                            <el-input
                                :disabled="checked_data"
                                v-model="share_form.keyWord" 
                                placeholder="输入分享文件的新关键字">
                            </el-input>
                        </el-form-item>
                    </div>
                </el-form>
            </div>
        </div>
        <div slot="footer" class="dialog_footer">
            <el-button type="primary" @click="on_share" :loading="loading">确 定</el-button>
            <el-button @click="on_cancel" type="danger" plain>取 消</el-button>
        </div>
    </el-dialog>
</div>
</template>

<script>
import List_item_cp from '@/components/public_cp/list_item'
export default {   //分享文件对话框
    components:{List_item_cp},
    data() {
        return {
            share_form:{
                beUser:'',
                keyWord:'',
            },
            loading:false,
            checked_data:false,
        };
    },
    watch:{
        checked_data(){
            this.share_form.keyWord='';
            this.$refs.share_form.clearValidate('keyWord');
        },
    },
    created(){
    },
    methods: {
        on_share(){ //文件分享执行方法
            this.$refs.share_form.validate((valid)=>{
                if (valid) {
                    this.loading = true;
                    let fileNames=this.$store.state.selection_files.map(item=>{
                        return item.fileName;
                    }) || [];
                    let fileShare=[];
                    this.$store.state.selection_files.forEach(item=>{
                        if(this.checked_data){ //如果选择了保留关键字的话执行
                            fileShare.push({
                                fileName:item.fileName,
                                keyWord:item.fileTag,
                            });
                        }else{
                            fileShare.push({
                                fileName:item.fileName,
                                keyWord:this.share_form.keyWord,
                            });
                        }
                    });
                    this.$axios.json_post('shared/sharelist',{
                        beUser:this.share_form.beUser,
                        fileShare:fileShare,
                    }).then(({data})=>{
                        for(let a of data){
                            setTimeout(() => {
                                this.$store.commit('add_share_file',{
                                    file:{
                                        name:a.fileName || a.msg,
                                    },
                                    msg:a.msg || "分享给用户："+a.beUser+' 成功',
                                    state:a.sate==1?'success':'fail',
                                });
                            },0);
                        }
                        this.$refs.share_form.resetFields();
                        this.$store.commit('set_share_dialog',false);
                    }).catch((msg)=>{
                        this.$message({
                            type: 'error',
                            message: msg,
                            offset:65,
                            showClose:true,
                        });
                    }).finally(()=>{
                        this.loading = false;
                    });
                } else {
                    return false;
                }
            });
        },
        on_cancel(){
            this.$refs.share_form.resetFields();
            this.$store.commit('set_share_dialog',false);
        },
        test_file_keyword(rule, value, callback){ //文件关键字输入验证
            if (!this.checked_data && !value) {
                callback(new Error('请输入关键字'));
            } else {
                callback();
            }
        },
    }
};
</script>
<style lang="scss" scoped>
.container{
    width:100%;
    display: flex;
    justify-content: space-between;
    height: fit-content;
    margin-bottom: 20px;
    >.left{
        width:100%;
        min-height: 100%;
        .top{
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 20px;
            >.left{
                width: 100%;
                display: flex;
                align-items: center;
                font-size: 14px;
                color: #000;
                line-height: 100%;
                flex-wrap: wrap;
                i{
                    width: 40px;
                    height: 40px;
                    margin-right: 10px;
                }
                >div{
                    width: fit-content;
                    margin-right: 10px;
                    margin-bottom: 5px;
                }
            }
        }
        .centre{
            width: 100%;
            margin-bottom: 20px;
            span{
                margin-left: 10px;
                min-width: fit-content;
                font-size: 15px;
                line-height: 100%;
                color: rgb(128, 128, 128);
            }
        }
        .bottom{
            width: 100%;
            display: flex;
            justify-content:row;
            align-items: center;
            margin-top: 10px;
            span{
                margin-left: 10px;
                min-width: fit-content;
                font-size: 15px;
                line-height: 100%;
                color: rgb(128, 128, 128);
            }
        }
    }
}
/deep/.dialog_footer{
    /deep/button{
        width: 90px;
        height: 35px;
        border-radius: 2px;
    }
}
</style>